home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / PROGS / GLE / TAPER.C < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  4.8 KB  |  219 lines

  1. /* 
  2.  * taper.c
  3.  * 
  4.  * FUNCTION:
  5.  * Draws a tapered screw shape.
  6.  *
  7.  * HISTORY:
  8.  * -- created by Linas Vepstas October 1991
  9.  * -- heavily modified to draw more texas shapes, Feb 1993, Linas
  10.  * -- converted to use GLUT -- December 1995, Linas
  11.  *
  12.  */
  13.  
  14. /* required include files */
  15. #include <stdlib.h>
  16. #include <math.h>
  17. #include <GL/glut.h>
  18. #include <GL/tube.h>
  19.  
  20. /* Some <math.h> files do not define M_PI... */
  21. #ifndef M_PI
  22. #define M_PI 3.14159265358979323846
  23. #endif
  24.  
  25. /* =========================================================== */
  26.  
  27. #define SCALE 3.33333
  28. #define CONTOUR(x,y) {                    \
  29.    double ax, ay, alen;                    \
  30.    contour[i][0] = SCALE * (x);                \
  31.    contour[i][1] = SCALE * (y);                \
  32.    if (i!=0) {                        \
  33.       ax = contour[i][0] - contour[i-1][0];        \
  34.       ay = contour[i][1] - contour[i-1][1];        \
  35.       alen = 1.0 / sqrt (ax*ax + ay*ay);        \
  36.       ax *= alen;   ay *= alen;                \
  37.       norms [i-1][0] = ay;                \
  38.       norms [i-1][1] = -ax;                \
  39.    }                            \
  40.    i++;                            \
  41. }
  42.  
  43. #define NUM_PTS (25)
  44.  
  45. double contour [NUM_PTS][2];
  46. double norms [NUM_PTS][2];
  47.  
  48. void init_contour (void)
  49. {
  50.    int i;
  51.  
  52.    /* outline of extrusion */
  53.    i=0;
  54.    CONTOUR (1.0, 1.0);
  55.    CONTOUR (1.0, 2.9);
  56.    CONTOUR (0.9, 3.0);
  57.    CONTOUR (-0.9, 3.0);
  58.    CONTOUR (-1.0, 2.9);
  59.  
  60.    CONTOUR (-1.0, 1.0);
  61.    CONTOUR (-2.9, 1.0);
  62.    CONTOUR (-3.0, 0.9);
  63.    CONTOUR (-3.0, -0.9);
  64.    CONTOUR (-2.9, -1.0);
  65.  
  66.    CONTOUR (-1.0, -1.0);
  67.    CONTOUR (-1.0, -2.9);
  68.    CONTOUR (-0.9, -3.0);
  69.    CONTOUR (0.9, -3.0);
  70.    CONTOUR (1.0, -2.9);
  71.  
  72.    CONTOUR (1.0, -1.0);
  73.    CONTOUR (2.9, -1.0);
  74.    CONTOUR (3.0, -0.9);
  75.    CONTOUR (3.0, 0.9);
  76.    CONTOUR (2.9, 1.0);
  77.  
  78.    CONTOUR (1.0, 1.0);   /* repeat so that last normal is computed */
  79. }
  80.    
  81. /* =========================================================== */
  82.  
  83. #define PSIZE 40
  84. double path[PSIZE][3];
  85. double twist[PSIZE];
  86. double taper[PSIZE];
  87.  
  88. void init_taper (void) {
  89.    int j;
  90.    double z, deltaz;
  91.    double ang, dang;
  92.  
  93.    z = -10.0;
  94.    deltaz = 0.5;
  95.  
  96.    ang = 0.0;
  97.    dang = 20.0;
  98.    for (j=0; j<40; j++) {
  99.       path[j][0] = 0x0;
  100.       path[j][1] = 0x0;
  101.       path[j][2] = z;
  102.  
  103.       twist[j] = ang;
  104.       ang += dang;
  105.  
  106.       taper[j] = 0.1 * sqrt (9.51*9.51 - z*z);
  107.  
  108.       z += deltaz;
  109.    }
  110.  
  111.    taper[0] = taper[1];
  112.    taper[39] = taper[38];
  113.  
  114. }
  115.  
  116. /* =========================================================== */
  117.  
  118. extern float lastx;
  119. extern float lasty;
  120.  
  121. void InitStuff (void)
  122. {
  123.    int style;
  124.  
  125.    /* configure the pipeline */
  126.    style = TUBE_JN_CAP;
  127.    style |= TUBE_CONTOUR_CLOSED;
  128.    style |= TUBE_NORM_FACET;
  129.    style |= TUBE_JN_ANGLE;
  130.    gleSetJoinStyle (style);
  131.  
  132.    lastx = 121.0;
  133.    lasty = 121.0;
  134.  
  135.    init_contour();
  136.    init_taper();
  137. }
  138.  
  139. /* =========================================================== */
  140.  
  141. void gleTaper (int ncp, 
  142.                gleDouble contour[][2], 
  143.                gleDouble cont_normal[][2], 
  144.                gleDouble up[3],
  145.                int npoints,
  146.                gleDouble point_array[][3],
  147.                float color_array[][3],
  148.                gleDouble taper[],
  149.                gleDouble twist[])
  150. {
  151.    int j;
  152.    gleAffine *xforms;
  153.    double co, si, angle;
  154.  
  155.    /* malloc the extrusion array and the twist array */
  156.    xforms = (gleAffine *) malloc (npoints * sizeof(gleAffine));
  157.  
  158.    for (j=0; j<npoints; j++) {
  159.       angle = (M_PI/180.0) * twist[j];
  160.       si = sin (angle);
  161.       co = cos (angle);
  162.       xforms[j][0][0] = taper[j] * co;
  163.       xforms[j][0][1] = - taper[j] * si;
  164.       xforms[j][0][2] = 0.0;
  165.       xforms[j][1][0] = taper[j] * si;
  166.       xforms[j][1][1] = taper[j] * co;
  167.       xforms[j][1][2] = 0.0;
  168.    }
  169.  
  170.    gleSuperExtrusion (ncp,               /* number of contour points */
  171.                 contour,    /* 2D contour */
  172.                 cont_normal, /* 2D contour normals */
  173.                 up,           /* up vector for contour */
  174.                 npoints,           /* numpoints in poly-line */
  175.                 point_array,        /* polyline */
  176.                 color_array,        /* color of polyline */
  177.                 xforms);
  178.  
  179.    free (xforms);
  180. }
  181.  
  182. /* =========================================================== */
  183.  
  184. void DrawStuff (void) {
  185.    int j;
  186.    double ang, dang;
  187.    double z, deltaz;
  188.    double ponent;
  189.    z=-1.0;
  190.    deltaz = 1.999/38;
  191.    ang = 0.0;
  192.    dang = lasty/40.0;
  193.    ponent = fabs (lastx/540.0);
  194.    for (j=1; j<39; j++) {
  195.       twist[j] = ang;
  196.       ang += dang;
  197.  
  198.       taper[j] = pow ((1.0 - pow (fabs(z), 1.0/ponent)), ponent);
  199.       z += deltaz;
  200.    }
  201.  
  202.    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  203.    glColor3f (0.5, 0.6, 0.6);
  204.  
  205.    /* set up some matrices so that the object spins with the mouse */
  206.    glPushMatrix ();
  207.    glTranslatef (0.0, 0.0, -80.0);
  208.    glRotatef (130.0, 0.0, 1.0, 0.0);
  209.    glRotatef (65.0, 1.0, 0.0, 0.0);
  210.  
  211.    /* draw the brand and the handle */
  212.    gleTaper (20, contour, norms,  NULL, 40, path, NULL, taper, twist);
  213.  
  214.    glPopMatrix ();
  215.    glutSwapBuffers ();
  216. }
  217.  
  218. /* ===================== END OF FILE ================== */
  219.